package com.oritsh.dicom.utils;

import com.oritsh.dicom.exception.ConnectionException;
import com.oritsh.dicom.exception.CreateSCUException;
import com.oritsh.dicom.scu.DicomSCU;
import com.oritsh.dicom.scu.EchoSCU;
import com.oritsh.dicom.scu.FindSCU;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * Created with IntelliJ IDEA.
 * User: zarra
 * Date: 14-2-5
 * Time: 下午7:07
 * Copyright Shanghai Orient Rain Information Technology Co.,Ltd.
 */
public class SCUFactory {

    private String aeCalling;
    private String aeCalled;
    private String remoteHost;
    private int remotePort;

    public String getAeCalling() {
        return aeCalling;
    }

    public void setAeCalling(String aeCalling) {
        this.aeCalling = aeCalling;
    }

    public String getAeCalled() {
        return aeCalled;
    }

    public void setAeCalled(String aeCalled) {
        this.aeCalled = aeCalled;
    }

    public String getRemoteHost() {
        return remoteHost;
    }

    public void setRemoteHost(String remoteHost) {
        this.remoteHost = remoteHost;
    }

    public int getRemotePort() {
        return remotePort;
    }

    public void setRemotePort(int remotePort) {
        this.remotePort = remotePort;
    }

    public static DicomSCU createSCU(Class<? extends DicomSCU> tClass, String aeCalling)
            throws CreateSCUException {

        try {
            Constructor<? extends DicomSCU> constructor = tClass.getConstructor(String.class);
            DicomSCU scu = constructor.newInstance(aeCalling);
            return scu;
        } catch (InstantiationException | IllegalAccessException |
                InvocationTargetException|NoSuchMethodException e) {
            throw new CreateSCUException(e.getMessage());
        }

    }

    public DicomSCU createSCU(Class<? extends DicomSCU> tClass)
            throws CreateSCUException, ConnectionException {
        DicomSCU scu =  createSCU(tClass,aeCalling);
        scu.connect(remoteHost,remotePort,aeCalled);
        return scu;
    }

    public EchoSCU echoSCU() throws CreateSCUException, ConnectionException {
        DicomSCU scu = createSCU(EchoSCU.class);
        return (EchoSCU) scu;
    }
    public FindSCU findSCU() throws CreateSCUException, ConnectionException {
        DicomSCU scu = createSCU(FindSCU.class);
        return (FindSCU) scu;
    }
}
